﻿/*----------------------------------------------------------------
           // Copyright (C) 2008 安华顺通(北京)科技有限公司
           // 版权所有。 
           //
           // 文件名：TableContextMenu.cs
           // 文件功能描述：具体表的右键菜单。
           //
           // 
           // 创建标识：杨波
           //
           // 修改标识：
           // 修改描述：
           //
           // 修改标识：
           // 修改描述：
//----------------------------------------------------------------*/

#region UsingStatements

using System;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using RuleCode.compiler;

#endregion

namespace RuleCode.ObjectTree
{
    public partial class TableContextMenu : DeleteAndCompileContextMenu
    {
        //private MySharp.MySharpIDE ide;
        protected ToolStripMenuItem editTableClass;

        public TableContextMenu()
        {
            InitializeComponent();
            editTableClass = new ToolStripMenuItem();
            editTableClass.Size = new Size(152, 22);
            editTableClass.Text = "EditTableClass";
            editTableClass.Click += editTableClassToolStripMenuItem_Click;
            baseContextMenuStrip.Items.Insert(2, editTableClass);
            OnAfterNodeDelete += TableContextMenu_OnAfterNodeDelete;
        }

        private void TableContextMenu_OnAfterNodeDelete(object sender, DeleteNodeEventArgs e)
        {
            //删除Table的源代码

            if (e.treeNode.Property == null)
            {
                Util.SetProerptyInSilent(e.treeNode);
            }
            string id = e.treeNode.Property.ID;
            string name = e.treeNode.Text;
            FileInfo file = new FileInfo(Util.GetTablesPath +"\\" + name + ".cs"); // (Util.OTPath + @"\Module\Tables" + "\\" + name + ".cs");
            if (file.Exists)
            {
                file.Delete();
            }
            FileInfo designFile = new FileInfo(Util.GetTablesPath + "\\" + name + ".Designer.cs");// (Util.OTPath + @"\Module\Tables" + "\\" + name + ".Designer.cs");
            if (designFile.Exists)
            {
                designFile.Delete();
            }

            FileInfo queryFile = new FileInfo(Util.GetQueriesPath + "\\" + id + ".cs");// (Util.OTPath + @"\Module\Queries\" + id + ".cs");
            if (queryFile.Exists)
            {
                queryFile.Delete();
            }
            //throw new NotImplementedException();
        }

        private void editTableClassToolStripMenuItem_Click(object sender, EventArgs e)
        {                        
            //ide = ide ?? new MySharp.MySharpIDE();
            //Util.IDE = ide;
            //ide.OpenFile( Util.GetTablesPath + "\\" + Util.SelectedNode.Text + ".cs");//(Util.OTPath + @"\Module\Tables" + "\\" + Util.SelectedNode.Text + ".cs");
            //ide.MdiParent = Util.MdiParent;


            //ide.Show();
        }

        protected override void ExecSql()
        {
            //base.ExecSql();  不需要执行父类的Sql语句
            SqlHelper sqlHelp = SqlHelper.Instance;
            string tableName = Util.SelectedNode.Text;
            sqlHelp.ExecuteNonQuery(CommandType.Text,
                                    "IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" +
                                    tableName + "]') AND type in (N'U'))DROP TABLE [dbo].[" + tableName + "]");
        }

        protected override void CompileNode()
        {
            base.CompileNode();
            SysCompiler.Instance.compileTable(Util.SelectedNode);
        }
    }
}